<?php
/**
 * File: Setting.php
 * User: Mr.Yang
 * Date: 2025/4/25 0025
 * Time: 11:17
 * Note:
 */

namespace app\admin\controller;


class Setting extends Base
{
    private $model;
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\Setting();
    }

    public function index()
    {
        $type = 'base';
        $setting = $this->model->getList($type);
        if (!$setting) {
            $setting = array(
                'sitename' => '',
                'icp' => '',
                'copyright' => '',
            );
        }
        $this->assign('type', $type);
        $this->assign('setting', $setting);
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post('data/a', []);
            $type = $this->request->post('type', '');
            $this->model->del($type);
            $count = 0;
            foreach ($data as $k => $v) {
                $setting = array(
                    'type' => $type,
                    'item' => $k,
                    'value' => $v
                );
                $res = $this->model->insert($setting);
                if ($res) {
                    $count++;
                }
            }
            if ($count == count($data)) {
                $this->success('添加成功');
            }
            $this->error('添加失败');
        }
    }
}